<?php
/**
 * App_Ro
 *
 * @package    App
 * @subpackage App_Ro
 */

/**
 * ユーザーリソース
 *
 * @category   BEAR
 * @package    App
 * @subpackage App_Ro
 * @author     $Author: anonymous $ <anonymous@example.com>
 * @version    SVN: Release: $Id:$
 */
class App_Ro_User extends App_Ro
{

    /**
     * テーブル名
     *
     * @var string
     */
    protected $_table = App_Ro::TABLE_USER;

    /**
     * コンストラクタ
     */
    public function __construct(array $config)
    {
        parent::__construct($config);
    }

    /**
     * インジェクタ
     */
    public function onInject()
    {
        parent::onInject();
        $this->_queryConfig['pager'] = 1;
        $this->_queryConfig['perPage'] = 10;
        $this->_query = BEAR::dependency('BEAR_Query', $this->_queryConfig, false);
    }

    /**
     * リソース作成
     *
     * @required user_name
     * @required password
     * @required email
     */
    public function onCreate($values)
    {
        $values['created_at'] = _BEAR_DATETIME; //現在時刻
        $result = $this->_query->insert($values);
        if ($this->_query->isError($result, MDB2_ERROR_CONSTRAINT)) {
            throw new Panda_Exception('IDが重複しています', 409);
        } elseif ($this->_query->isError($result)) {
            throw new Panda_Exception('登録できませんでした', 500);
        }
        return $result;
    }

    /**
     * リソース変更
     *
     * @required id
     * @required user
     * @optional profile
     *
     * @aspect around App_Aspect_Transaction
     */
    public function onUpdate($values)
    {
        $params = $values['user'];
        $params['updated_at'] = _BEAR_DATETIME;
        $where = 'id = ' . $this->_query->quote($values['id'], 'integer');
        $result = $this->_query->update($params, $where);
        if (!isset($values['profile'])) {
            return $result;
        }
        // profile
        $params = $values['profile'];
        $where = 'user_id = ' . $this->_query->quote($values['id'], 'integer');
        $params['updated_at'] = _BEAR_DATETIME;
        $result = $this->_query->update($params, $where, App_Ro::TABLE_PROFILE);
        if (!$result) {
            // updateできなかったらinsert
            unset($params['updated_at']);
            $params['user_id'] = $this->_query->quote($values['id'], 'integer');
            $params['created_at'] = _BEAR_DATETIME;
            $result = $this->_query->insert($params, App_Ro::TABLE_PROFILE);
        }
        return $result;
    }

    /**
     * リソース読み込み
     *
     */
    public function onRead($values)
    {
        $where = isset($values['id']) ? 'WHERE id = ' . $this->_query->quote($values['id'], 'integer') : '';
        $sql = "SELECT * FROM {$this->_table} {$where}";
        if (isset($values['id'])) {
            // _db(MDB2オブジェクト）でセレクト
            $result = $this->_db->queryRow($sql);
        } else {
            // DBページャーを使うために_query(BEAR_Queryオブジェクト）でセレクト
            $result = $this->_query->select($sql);
        }
        return $result;
    }

    /**
     * リソース削除
     *
     * @required id
     */
    public function onDelete($values)
    {
        $values['deleted_at'] = _BEAR_DATETIME;
        $where = 'id = ' . $this->_query->quote($values['id'], 'integer');
        $result = $this->_query->update($values, $where);
        return $result;
    }

    /**
     * リンク
     *
     * @return array
     *
     * @required id
     */
    function onLink($values)
    {
    	$links = array();
        $links['profile'] = "User/Profile?user_id={$values['id']}";
        $links['entry_latest'] = "Entry/User/Latest?user_id={$values['id']}";
        $links['follower_to'] = "User/Follower/To?user_id={$values['id']}";
        $links['follower_from'] = "User/Follower/From?user_id={$values['id']}";
        return $links;
    }
}